package pro.spring.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pro.spring.inter.ThemeMapper;
import pro.spring.inter.UdiscussMapper;
import pro.spring.inter.UsupportMapper;
import pro.spring.inter.UsersMapper;
import pro.spring.model.Udiscuss;
import pro.spring.vo.PaginationVo;

@Service
public class UdiscussService{

	@Autowired
	private UdiscussMapper UdiscussMapper;
	
	@Autowired
	private UsersMapper UsersMapper;
	
	@Autowired
	private ThemeMapper ThemeMapper;
	
	@Autowired
	private UsupportMapper UsupportMapper;
	
	public int deleteByPrimaryKey(Integer id) {
		return UdiscussMapper.deleteByPrimaryKey(id);
	}
	
	public int deleteByXianxia(Integer drId,Integer uId) {
		return UdiscussMapper.deleteByXianxia(drId, uId);
	}

	public int insert(Udiscuss record) {
		record.setDsContent(record.getDsContent().replace("contenteditable=\"true\"", " "));
		return UdiscussMapper.insert(record);
	}
	
	public int selectCountBydrId(Integer id) {
		return UdiscussMapper.selectCountBydrId(id);
	}

	public Udiscuss selectByPrimaryKey(Integer id) {
		return UdiscussMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKey(Udiscuss record) {
		return UdiscussMapper.updateByPrimaryKey(record);
	}
	
	public List<Udiscuss> selectBydrId(Integer drId,Integer pagesize) {
		List<Udiscuss> list = UdiscussMapper.selectBydrId(drId, 0, pagesize);
		for(Udiscuss u:list){
			u.setUsers(UsersMapper.selectByPrimaryKey(u.getUsersUId()));
		}
		return list;
	}
	
	public PaginationVo<Udiscuss> getPaginationInfo(int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscuss> pageVo=new PaginationVo<Udiscuss>();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(UdiscussMapper.selectCount());
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		pageVo.setList(UdiscussMapper.selectListPage(pageVo.getOffset(), pageVo.getRows()));
		return pageVo;
	}
	
	//一个主题讨论版
	public PaginationVo<Udiscuss> getThemeOne(Integer thId,int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscuss> pageVo=new PaginationVo<Udiscuss>();
		Integer drId = ThemeMapper.selectByPrimaryKey(thId).getUdiscusserDrId();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(UdiscussMapper.selectCountBydrId(drId));
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		List<Udiscuss> list = UdiscussMapper.selectBydrId(drId,pageVo.getOffset(), pageVo.getRows());
		for(Udiscuss u:list){
			u.setUsers(UsersMapper.selectByPrimaryKey(u.getUsersUId()));
			u.setSuport(UsupportMapper.selectCountBydsId(u.getDsId()));
		}
		pageVo.setList(list);
		return pageVo;
	}

	
}
